<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class controllertransformation //extends AppController
{

	function __construct(  )
	{
		
		$task	=	application::getTask();//( empty($_REQUEST['task'])? '':$_REQUEST['task']);
		
		switch( $task ){
			
			
			
				
			case 'getList':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getList(
											application::getUser_id(),
											$parameters->view_id,
											$parameters->column,
											$parameters->order,
											$parameters->limit,
											$parameters->offset);
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
			    //catch (Exception $e) {
			    //    echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			    //}
				
		
				
				
				break;
					
			case 'getItem':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getItem(
													application::getUser_id(),
													$parameters->id);

					if($result){
						
						$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
					}else{
						
						$result=array("status"=>0,
									"data"=>MSG_31_SEM_ITENS_RETORNADOS,
									"notification"=>31);
					}

											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
				
				
				break;	
					
			case 'save':
				
				
				try {
        						
								
					$parameters		=	application::getParameters();
								
					$id				=	$parameters->id;
					
					if(empty($id)){
						//sem id, então não é para criar um novo
		
						if( $this->save($parameters) ){
		
							$result=array("status"=>1,
					"data"=>MSG_32_REGRA_DE_TRANSFORMACAO_SALVA_COM_SUCESSO,
									"notification"=>32);
									
						}else{
							//erro ao salvar
							$result=array("status"=>0,
						"data"=>MSG_33_REGRA_DE_TRANSFORMACAO_NAO_SALVA,
									"notification"=>33);
									
						}
					
			
					}else{
						
						$this->save($parameters);
						
						$result=array("status"=>1,
						"data"=>MSG_32_REGRA_DE_TRANSFORMACAO_SALVA_COM_SUCESSO,
									"notification"=>32);
					}
					
					
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }

		
				
				
				break;					
			
				
			case 'delete':
				
				
				try {
        						
								
					$parameters		=	application::getParameters();
								
					$id				=	$parameters->id;
					
					if(!empty($id)){
						//sem id, então não é para criar um novo
		
						if( $this->delete(application::getUser_id(),
												$parameters->id) ){
							
							//$result=array("status"=>"delete sucess");
							$result=array("status"=>1,
						"data"=>MSG_34_REGRA_DE_TRANSFORMACAO_EXCLUIDA_SUCESSO,
									"notification"=>18);
									
						}else{
							//erro ao salvar
							//$result=array("status"=>"delete failed");
							$result=array("status"=>0,
						"data"=>MSG_35_REGRA_DE_TRANSFORMACAO_FALHA_AO_EXCLUIDA,
									"notification"=>35);
						}
					
			
					}else{

						//$result=array("status"=>"failed not exist");
						$result=array("status"=>0,
						"data"=>MSG_35_REGRA_DE_TRANSFORMACAO_FALHA_AO_EXCLUIDA,
									"notification"=>35);
					}
					
					
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
			  //  catch (Exception $e) {
			  //      echo "#Erro Fatal ('{$e->getMessage()}')\n{$e}\n";
			   // }
				
		
				
				
				break;		
			case 'clone':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->cloneItem(application::getUser_id(),
													$parameters->id);
					
					if( $result ){
						
						$result=array("status"=>1,
									"data"=>MSG_37_REGRA_TRANSFORMACAO_CLONE_SUCESSO,
									"notification"=>37);
					}else{
						
						$result=array("status"=>0,
									"data"=>MSG_38_REGRA_TRANSFORMACAO_CLONE_FALHA,
									"notification"=>38);
					}
		
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    }
		
				
				
				break;	
			case 'execute':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
					
								
					$result 	=	$this->executeItem(
													application::getUser_id(),
													$parameters->id);
					
					if( $result ){
						
						$result=array("status"=>1,
							"data"=>MSG_39_REGRA_TRANSFORMACAO_EXECUTE_SUCESSO,//application::arrayToJson($result),
									"notification"=>39);
					}else{
						
						$result=array("status"=>0,
							"data"=>MSG_40_REGRA_TRANSFORMACAO_EXECUTE_FALHA,
									"notification"=>40);
					}
					

	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );

			    }

				
				
				break;
				
			case 'executeAllItens':
				
				
				try {
        						
					//exige autenticacao de usuário
					application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
					
								
					$result 	=	$this->executeAllItens(
													application::getUser_id(),
													$parameters->view_id);
					
					if( $result ){
						
						$result=array("status"=>1,
					"data"=>MSG_41_REGRA_TRANSFORMACAO_EXECUTE_TODAS_SUCESSO,//application::arrayToJson($result),
									"notification"=>39);
					}else{
						
						$result=array("status"=>0,
						"data"=>MSG_42_REGRA_TRANSFORMACAO_EXECUTE_TODAS_FALHA,
									"notification"=>40);
					}
					

	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );

			    }

				
				
				break;	
			default:
				
				//echo "view não definida.";
				throw new AppException( MSG_23_METODO_NAO_DEFINIDO );
				
				break;
		}
		
	}
	

	
	
	
	public function getList( $user_id, $view_id, $column, $order, $limit, $offset ){
				
		$result=false;

		try{
			
			$model	=	application::getModel('transformation');
			
			$result	=	$model->getList( $user_id, $view_id,  
									$column, $order, $limit, $offset );

		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;

	}
	
	
	
	


	public function getItem( $user_id, $id ){
				
		$result=false;
		
		try{

			$model	=	application::getModel('transformation');
			
			$result	=	$model->getItem( $user_id, $id );
		
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		

		return $result;
		
	}
	
	
	
	public function delete( $user_id, $id ){
				
		$result=false;
				
		try {
        		
				
			$model	=	application::getModel('transformation');
	
			$result	=	$model->delete( $user_id, $id );
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
	
	
	public function save( $parameters ){
				
		$result=false;
				
		try {
			
			$model	=	application::getModel('transformation');
	
			$result	=	$model->save( $parameters );
		
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
	public function cloneItem( $user_id, $id ){
				
		$result=false;
		
		try {
				
			$model	=	application::getModel('transformation');
	
			$result	=	$model->cloneItem( $user_id, $id );
			
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }

		return $result;
		
	}
	
	
	public function executeItem( $user_id, $id ){
				
		$result=false;
		
		try {
			
			$model	=	application::getModel('transformation');
	
			$result	=	$model->executeItem( $user_id, $id );
		
		}
	    catch (AppException $e) {
			throw new AppException( $e->getMessage() );
	    	
	    }

		return $result;
		
	}
	
	
	
	
	public function executeAllItens( $user_id, $view_id ){
				
		$result=false;
		
		try {
			
			$model	=	application::getModel('transformation');
	
			$result	=	$model->executeAllItens( $user_id, $view_id );
		
		}
	    catch (AppException $e) {
			throw new AppException( $e->getMessage() );
	    	
	    }

		return $result;
		
	}
	
	
}